package com.oauth.utils;

import java.io.File;
import android.graphics.BitmapFactory;

public class FileHelper {
	public static final String PREFERENCES_NAME = "share_token";

	private static int computeInitialSampleSize(BitmapFactory.Options options,
			int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;
		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
				.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
				Math.floor(w / minSideLength), Math.floor(h / minSideLength));
		if (upperBound < lowerBound) {
			return lowerBound;
		}
		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}
	}

	/*
	 * public static BitmapFactory.Options getOpts(String mPicPath){
	 * BitmapFactory.Options opts = new BitmapFactory.Options();
	 * opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(mPicPath, opts);
	 * opts.inSampleSize = computeInitialSampleSize(opts, -1, 128*128);
	 * opts.inJustDecodeBounds = false; return opts; }
	 */

	public static BitmapFactory.Options getOpts(String mPicPath, int x, int y) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(mPicPath, opts);
		opts.inSampleSize = computeInitialSampleSize(opts, -1, x * y);
		opts.inJustDecodeBounds = false;
		return opts;
	}

	public static void mkdir(String path) {
		File f = new File(path);

		if (f.isFile()) {
			f.delete();
		}

		if (!f.exists()) {
			f.mkdirs();
		}
	}
}
